# -*- coding: utf-8 -*-
"""
    The following copyright and license notice apply only to:
        - those parts of this Turbogears 2 project which are not
          part of the stock quickstarted project
        - those parts which are not otherwise licensed otherwise

    Copyright (C) 2009 Ciaran Farrell <cfarrell1980@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

from tg import expose, flash, require, url, request,response, redirect,tmpl_context,validate,config
from pylons.i18n import ugettext as _, lazy_ugettext as l_
#from catwalk.tg2 import Catwalk
from repoze.what import predicates
from lifepyscle.lib.base import BaseController
from lifepyscle.model import DBSession, metadata
from lifepyscle.controllers.error import ErrorController
from lifepyscle import model
from lifepyscle.model import *
from lifepyscle.controllers.secure import SecureController
from lifepyscle.widgets.add_party  import parties_autocomplete_form
from lifepyscle.widgets.quick_autocomplete import parties_single_autocomplete_form
from lifepyscle.widgets import custom_validators
from lifepyscle.lib.uploader import uploadFile
uploads = config.get('uploads')
cat="signatory" # added to every returned dict


class SignatoryController(BaseController):
    @expose()
    def index(self,**kw):
        pass

    @expose('json')
    def updateSignatories(self,record_id,sigstring,**kw):
        # first get the record
        if not record_id.isdigit():
            flash("Bad record id","error")
            sigs=sigstring
        else:
            try:
                R = DBSession.query(Record).filter(Record.record_id=='%d'%int(record_id)).one()
            except:
                flash("Record with id %d not found"%int(record_id),"warn")
                sigs=sigstring
            else:
                existing_sigs = R.signatories
                # split the signatories string into individual signatories
                split_sigs = sigstring.split(",")
                if len(split_sigs)==0:
                    flash("Nothing to do","info")
                else:
                    for sig in split_sigs:
                        if sig not in existing_sigs:
                            try:
                                S = DBSession.query(Signatory).filter(Signatory.name=='%s'%sig).one()
                            except: # add the signatory
                                newsig = Signatory()
                                newsig.name == sig
                                DBSession.add(newsig)
                                R.signatories.append(newsig)
                            else:
                                R.signatories.append(sig)
                    sigs = ",".join(R.signatories)
                    flash("Updated signatories","info")
        return dict(record_id=record_id,sigstring=sigs)
